package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.PageDTO;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("list")
    public PageDTO list(@RequestParam Integer page, @RequestParam Integer size){
        PageDTO<Item> itemPageDTO = new PageDTO<>();
        Page<Item> ipage = new Page<>(page,size);
        LambdaQueryWrapper<Item> lqw = new LambdaQueryWrapper<>();
        itemService.page(ipage, lqw);
        itemPageDTO.setList(ipage.getRecords());
        itemPageDTO.setTotal(ipage.getTotal());
        return itemPageDTO;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item queryId(@PathVariable("id")Long id){
        Item byId = itemService.getById(id);
        return byId;
    }

    /**
     * 添加商品
     * @param item
     */
    @PostMapping
    public void add(@RequestBody Item item){
        itemService.save(item);
    }

    /**
     * 商品上架、下架
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void status(@PathVariable("id")Long id,@PathVariable("status")int status){
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        itemService.updateById(item);
    }

    @PutMapping
    public void  revise(@RequestBody Item item){
        itemService.updateById(item);
    }
    @DeleteMapping("/{id}")
    public void deleteId(@PathVariable("id")Long id){
        itemService.removeById(id);
    }
}
